package com.gxa.learning.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.*;
import com.gxa.learning.core.util.StringUtil;
import com.gxa.learning.pc.service.DepartmentService;
import com.gxa.learning.pc.service.QueTemplateService;
import com.gxa.learning.pc.service.TemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 操作问卷模板的接口
 * @Author ChenJ
 * @date 2021/12/02 16:12
 **/
@RestController
@RequestMapping("/queTemplate")
@Api(value = "QueTemplateController",tags = "操作问卷模板的接口")
public class QueTemplateController {

    @Autowired
    private QueTemplateService queTemplateService;

    @Autowired
    private TemplateService templateService;

    @Autowired
    private DepartmentService departmentService;

    @Transactional
    @PostMapping("/insert")
    @ApiOperation("添加问卷模板的接口")
    @ApiImplicitParam(name = "jsonData", value = "jsonData", paramType = "body")
    public Map<String, Object> insert(@RequestBody Map<String,Object> jsonData) throws BusinessException {

        // 数据校验
        if (jsonData == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }

        QueTemplate queTemplate = new QueTemplate();
        // 将前段前段传来数据中的问卷模板信息添加到对象上面
        if (jsonData.get("title")!=null){
            queTemplate.setTitle(jsonData.get("title").toString());
        }
        if (jsonData.get("explain")!=null){
            queTemplate.setExplain(jsonData.get("explain").toString());
        }
        if (jsonData.get("name")!=null){
            queTemplate.setName(jsonData.get("name").toString());
        }
        if (jsonData.get("part")!=null){
            queTemplate.setPart(jsonData.get("part").toString());
        }
        if (jsonData.get("status")!=null){
            queTemplate.setStatus(jsonData.get("status").toString());
        }
        if (jsonData.get("user")!=null){
            queTemplate.setUser(jsonData.get("user").toString());
        }
        // 将上传时间设置为当前时间
        queTemplate.setTime(new Date());

        // 获取前段传来的数据中的问卷题目信息
        Object templates = jsonData.get("templates");
        List<Map<String,Object>> template1 = (List) templates;

        // 添加问卷模板操作
        QueTemplate insert = queTemplateService.insert(queTemplate);

        // 遍历添加问卷模板的题目
        for (Map<String,Object> questionOne : template1) {
            questionOne.put("questionnaireId",insert.getId());
            Template template = new Template();
            // 关联问卷模板编号
            template.setQuestionnaireId(insert.getId());

            // 获取前段传来的数据中的问卷题目信息添加到对象上面
            if (questionOne.get("type")!=null){
                template.setType(Integer.valueOf(questionOne.get("type").toString()));
            }
            if (questionOne.get("question")!=null){
                template.setQuestion(questionOne.get("question").toString());
            }
            if (questionOne.get("explain")!=null){
                template.setExplain(questionOne.get("explain").toString());
            }
            if (questionOne.get("star")!=null && questionOne.get("star")!=""){
                template.setStar(Integer.valueOf(questionOne.get("star").toString()));
            }
            if (questionOne.get("answer")!=null){
                template.setAnswer(questionOne.get("answer").toString());
            }

            templateService.insert(template);

        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/select")
    @ApiOperation("获取问卷模板列表的接口（包含模糊查询）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "page", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "size", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "queTemplate", value = "queTemplate", dataType = "QueTemplate", paramType = "body", dataTypeClass = QueTemplate.class)
    })
    public Map<String, Object> select(@RequestParam(value = "page",required = false) Integer page, @RequestParam(value = "size",required = false) Integer size, @RequestBody QueTemplate queTemplate) throws IllegalAccessException, BusinessException {

        // 先进行数据校验
        if (queTemplate==null){
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }
        if (page== null){
            page = 1;
        }
        if (size == null){
            size=10;
        }

        PageInfo<QueTemplate> queTemplatePageInfo = queTemplateService.selectByPage(page, size, queTemplate);
        // 获取全部数据
        List<QueTemplate> list = queTemplatePageInfo.getList();
        // 如果没有数据则抛出没有数据异常
        if (list.size()==0){
            throw new BusinessException(ExceptionCode.WRONG_DATA);
        }
        // 获取总数
        long total = queTemplatePageInfo.getTotal();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(list);
        jsonResult.put("count",total);
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("修改问卷模板的接口")
    @ApiImplicitParam(name = "queTemplate", value = "queTemplate", dataType = "QueTemplate", paramType = "body",dataTypeClass = QueTemplate.class)
    public Map<String, Object> update(@RequestBody QueTemplate queTemplate) throws BusinessException {

        // 进行数据校验
        if (queTemplate==null){
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }
        if (queTemplate.getId()==null){
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }

        // 进行修改操作
        Integer integer = queTemplateService.updateById(queTemplate);
        // 没有修改成功
        if (integer==0) {
            throw new BusinessException(ExceptionCode.WRONG_UPLOAD);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation("删除问卷模板的接口")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Map", paramType = "body")
    @Transactional
    public Map<String, Object> delete(@RequestBody  Map<String,Object> id) throws BusinessException {

        if (id == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }

        // 删除问卷模板操作
        Integer integer = queTemplateService.deleteById(Long.valueOf(id.get("id").toString()));

        Template template = new Template();
        template.setQuestionnaireId(Long.valueOf(id.get("id").toString()));

        // 遍历删除问卷题目中题目的操作
        List<Template> select = templateService.select(template);
        for (Template findTemplate : select) {
            templateService.deleteById(findTemplate.getId());
        }

        if (integer == 0){
            throw new BusinessException(ExceptionCode.WRONG_DELETE);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

}
